package com.ruoyi.worker.service.impl;

import com.ruoyi.worker.domain.DailySalary;
import com.ruoyi.worker.domain.DailySalaryModifyLog;
import com.ruoyi.worker.domain.DailySalaryTotal;
import com.ruoyi.worker.mapper.EmployeeSalaryMapper;
import com.ruoyi.worker.service.EmployeeSalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class EmployeeSalaryServiceImpl implements EmployeeSalaryService {

    @Autowired
    EmployeeSalaryMapper employeeSalaryMapper;

    @Override
    public List<DailySalaryTotal> listEmployeeSalary(DailySalaryTotal dailySalaryTotal) {
        return employeeSalaryMapper.listEmployeeSalary(dailySalaryTotal);
    }

    @Override
    public List<DailySalary> listEmployeeSalaryDetail(DailySalary dailySalary) {
        return employeeSalaryMapper.listEmployeeSalaryDetail(dailySalary);
    }

    @Override
    public List<DailySalary> listEmployeeSalaryDetail(Long[] salaryDailyTotalIds) {
        return employeeSalaryMapper.listEmployeeSalaryDetailByBillIds(salaryDailyTotalIds);
    }

    @Override
    public int insertDailySalaryTotal(DailySalaryTotal dailySalaryTotal) {
        return employeeSalaryMapper.insertDailySalaryTotal(dailySalaryTotal);
    }

    @Override
    public int transalteAttendanceToSalary(List<DailySalary> dailySalaries) {
        return employeeSalaryMapper.insertDailySalary(dailySalaries);
    }

    @Override
    @Transactional
    public int updateSalary(DailySalary dailySalary) {
        //修改临时工薪资
        int flag = employeeSalaryMapper.updateSalary(dailySalary);
        if(flag >0){
            //记录修改薪资日志
            DailySalaryModifyLog dailySalaryModifyLog = new DailySalaryModifyLog();
            dailySalaryModifyLog.setSalaryDailyId(dailySalary.getId());
            dailySalaryModifyLog.setCreateBy(dailySalary.getCreateBy());
            dailySalaryModifyLog.setSalary(dailySalary.getSalary());
            dailySalaryModifyLog.setNewSalary(dailySalary.getNewSalary());
            dailySalaryModifyLog.setIdCard(dailySalary.getIdCard());
            dailySalaryModifyLog.setWorkerName(dailySalary.getWorkerName());
            dailySalaryModifyLog.setWorkingDate(dailySalary.getWorkingDate());
            dailySalaryModifyLog.setAttendanceStatus(dailySalary.getAttendanceStatus());
            dailySalaryModifyLog.setCreateTime(new Date());
            dailySalaryModifyLog.setRemark(dailySalary.getRemark());
            flag=  employeeSalaryMapper.addModifySalaryLog(dailySalaryModifyLog);
            //修改汇总薪资
            DailySalaryTotal dailySalaryTotal =new DailySalaryTotal();
            dailySalaryTotal.setSalaryTotal(dailySalary.getNewSalary()-dailySalary.getSalary());
            dailySalaryTotal.setId(dailySalary.getSalaryDailyTotalId());
            flag=  employeeSalaryMapper.updateSalaryTotal(dailySalaryTotal);
        }
        return flag ;
    }

    @Override
    public List<DailySalaryModifyLog> listDailySalaryModifyLog(DailySalaryModifyLog dailySalaryModifyLog) {
        return employeeSalaryMapper.listDailySalaryModifyLog(dailySalaryModifyLog);
    }

}
